<?php
namespace Index\Controller;
use Org\Util\ArrayList;
use Think\Controller;
class IndexController extends Controller {
    public function __construct(){
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        header('Access-Control-Allow-Methods: GET, POST, PUT');
    }

    public function index(){
        $User = M("sensordata");
        $list = $User->where('id=2')->select();
        dump($list);
    }

    public function addgroup(){
        $name = $_GET["groupname"];
        $group = M("sensorgroup");
        $groupname = $group->where('groupname'."=".'"'.$name.'"')->select();

        if(count($groupname) == 0){
            $array = array(
                "group" => md5(date()),
                "groupname" => $name,
            );
            $group->add($array);
        }else{
            return $this->error("message has existed");
        }

        $result = array(
            "error"=>"0",
            "Message"=>"success",
        );
        echo json_encode($result);
    }

    public function delgroup(){
        $name = $_GET["groupname"];
        $group = M("sensorgroup");
        $groupname = $group->where('groupname'."=".'"'.$name.'"')->select();

        if(count($groupname) != 0){
            $group->where('groupname'."=".'"'.$name.'"')->delete();
        }else{
            return $this->error("message has existed");
        }

        $result = array(
            "error"=>"0",
            "Message"=>"success",
        );
        echo json_encode($result);
    }

    public function getgroup(){
        $group = M("sensorgroup");
        $grouparray = $group->select();

        echo json_encode($grouparray);
    }

    public function adddata(){
        $groupid = $_GET["group"];
        $sensorid = $_GET["sensorid"];
        $sensortype = $_GET["sensortype"];
        $sensordata = $_GET["sensordata"];

        if($groupid == null){
            return $this->error("no gourpid in database");
        }
        $sensor = M("sensordata");
        $map['group'] = $groupid;
        $map["sensorid"] = $sensorid;
        $map["sensortype"] = $sensortype;
        $map["sensordata"] = $sensordata;
        $isExist = $sensor->add($map);

        $result = array(
            "error"=>"0",
            "Message"=>"success"
        );
        echo json_encode($result);
    }

    public function addfault(){
        $groupid = $_GET["group"];
        $sensorid = $_GET["sensorid"];
        $sensortype = $_GET["sensortype"];
        $sensordata = $_GET["sensordata"];
        $errortype = $_GET["errortype"];
        if($groupid == null){
            return $this->error("no gourpid in database");
        }
        $sensor = M("sensordata");
        $map['group'] = $groupid;
        $map["sensorid"] = $sensorid;
        $map["sensortype"] = $sensortype;
        $map["sensordata"] = $sensordata;
        $map["errortype"] = $errortype;
        $isExist = $sensor->add($map);

        $result = array(
            "error"=>"0",
            "Message"=>"success"
        );
        echo json_encode($result);
    }

    /*
    public function deldata(){
        $groupid = $_GET["group"];
        $sensorid = $_GET["sensorid"];
        $sensortype = $_GET["sensortype"];

        $sensor = M("sensordata");
        $map['group'] = $groupid;
        $map['sensorid'] = $sensorid;
        $map['sensortype']=$sensortype;
        $sensornode = $sensor->where($map)->delete();

        if(count($sensornode) == 0){
            return $this->error("no data in database");
        }

        $result = array(
            "error"=>"0",
            "value"=> array("Message"=>"success"),
        );
        echo json_encode($result);
    }

    public function updatedata(){
        $groupid = $_GET["group"];
        $sensorid = $_GET["sensorid"];
        $sensortype = $_GET["sensortype"];
        $sensordata = $_GET["sensordata"];
        $sensor = M("sensordata");
        $map['group'] = $groupid;
        $map['sensorid'] = $sensorid;
        $map['sensortype']=$sensortype;
        $data['sensordata'] = $sensordata;
        $sensornode = $sensor->where($map)->save($data);
    }
    */

    public function error($message){
        $result = array(
            "error"=>"1",
            "Message"=>$message,
        );
        echo json_encode($result);
    }
}